package com.cetool.autocms.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger配置文件
 *
 * @author 端木超群
 * @date 2021/11/05
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {
    /**
     * token名
     */
    private static final String TOKEN_NAME = "token";

    /**
     * token描述
     */
    private static final String TOKEN_DESCRIPTION = "令牌";

    /**
     * 参考类型
     */
    private static final String MODE_REFERENCE = "string";

    /**
     * 参数类型
     */
    private static final String PARAMETER_TYPE = "header";

    /**
     * swagger配置文件参数名
     */
    private static final String SWAGGER_ENABLE = "swagger.enable";

    /**
     * swagger扫描路径包
     */
    private static final String BASE_PACKAGE = "com.cetool.autocms.controller";

    /**
     * 所有路径
     */
    private static final String ALL_PATH = "/.*";

    /**
     * 分组版本
     */
    private static final String GROUP_NAME = "WEB接口文档1.0版本";

    /**
     * 创建api应用
     *
     * @return api应用
     */
    @Bean(value = "defaultApi2")
    public Docket createRestApi() {
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();
        parameterBuilder.name(TOKEN_NAME)
                .description(TOKEN_DESCRIPTION)
                .modelRef(new ModelRef(MODE_REFERENCE))
                .parameterType(PARAMETER_TYPE)
                .required(false)
                .build();
        parameters.add(parameterBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .apiInfo(apiInfo()).pathMapping("/")
                .groupName(GROUP_NAME)
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
                .paths(PathSelectors.regex(ALL_PATH)).build()
                .globalOperationParameters(parameters);
    }

    /**
     * 构建api的文档信息
     *
     * @return ApiInfo对象
     */
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("自动服务内容管理平台服务API接口测试平台")
                .description("自动服务内容管理平台所有Restful接口").termsOfServiceUrl("").version("1.0").build();
    }

}
